package com.baibei.pay.password;

import android.content.Context;

import com.baibei.basic.BasicPresenterImpl;
import com.baibei.ebec.sdk.ApiFactory;
import com.baibei.ebec.sdk.IPushApi;
import com.baibei.ebec.sdk.IUserApi;
import com.baibei.model.UserInfo;
import com.baibei.sdk.ApiDefaultObserver;
import com.baibei.sdk.Empty;
import com.rae.swift.session.SessionManager;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * 设置资金密码表现层
 * Created by shenminjie on 2017/8/15.
 * mail:shenminjie@100bei.com
 */

public class SetFundPwdPresenterImpl extends BasicPresenterImpl<SetFundPwdContract.SetFundPwdView> implements SetFundPwdContract.Presenter {

    /**
     * 接口
     */
    private IPushApi mPushApi;

    /**
     * 用户api
     */
    private IUserApi mUserApi;

    public SetFundPwdPresenterImpl(Context context, SetFundPwdContract.SetFundPwdView view) {
        super(context, view);
        mPushApi = ApiFactory.getInstance().getPushApi();
        mUserApi = ApiFactory.getInstance().getUserApi();
    }

    @Override
    public void getCheckCode() {
        String phone = mView.getPhoneNum();
        String type = mView.getCheckCodeType();
        createObservable(mPushApi.sendSMS(type, phone, null)).subscribe(new ApiDefaultObserver<Empty>() {
            @Override
            protected void onError(String s) {
                mView.onSmsFailed(s);
                mView.stopCountDown();
            }

            @Override
            protected void accept(Empty empty) {
                mView.onSmsSuccess();
            }
        });
    }

    @Override
    public void setFundPassword() {
        mView.showLoading();
        createObservable(mUserApi.setFundPassword(mView.getPassword(), mView.getCheckCode()))
                .flatMap(new Function<Empty, Observable<UserInfo>>() {
                    @Override
                    public Observable<UserInfo> apply(@NonNull Empty empty) throws Exception {
                        return createObservable(mUserApi.getUserInfo());
                    }
                }).subscribe(new ApiDefaultObserver<UserInfo>() {
            @Override
            protected void onError(String s) {
                mView.hideLoading();
                mView.onFailed(s);
            }

            @Override
            protected void accept(UserInfo userInfo) {
                mView.hideLoading();
                SessionManager.getDefault().setUser(userInfo);
                mView.onSuccess();
            }
        });
    }

    @Override
    public void resetFundPassword() {
        mView.showLoading();
        createObservable(mUserApi.resetFundPassword(mView.getPassword(), mView.getCheckCode()))
                .flatMap(new Function<Empty, Observable<UserInfo>>() {
                    @Override
                    public Observable<UserInfo> apply(@NonNull Empty empty) throws Exception {
                        return createObservable(mUserApi.getUserInfo());
                    }
                }).subscribe(new ApiDefaultObserver<UserInfo>() {
            @Override
            protected void onError(String s) {
                mView.hideLoading();
                mView.onFailed(s);
            }

            @Override
            protected void accept(UserInfo userInfo) {
                mView.hideLoading();
                SessionManager.getDefault().setUser(userInfo);
                mView.onSuccess();
            }
        });
    }
}
